#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
using namespace std;

int main() {

    string s;
    cin>>s;
    unordered_map<char,int> has;
    for(int i=0;i<s.size();++i){
        has[s[i]]=i;
    }
    int n;
    cin>>n;
    vector<string> vecArr(n);
    for(int i=0;i<n;++i){
        cin>>vecArr[i];
    }
    sort(vecArr.begin(),vecArr.end(),[&](const string& s1,const string& s2){

        int p1=0,p2=0;
        while(p1<s1.size() && p2<s2.size()){
            if(has[s1[p1]]!=has[s2[p2]]){
                return has[s1[p1]]<has[s2[p2]];
            }
            ++p1,++p2;
        }

        return s1.size()<s2.size();
    });

    for(int i=0;i<n;++i){
        cout<<vecArr[i]<<endl;
    }
    return 0;
}
